package com.example.todo.DAL;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.example.todo.DAL.IItemRepository;
import com.example.todo.Entities.Item;

public class ItemRepository extends SQLiteOpenHelper implements IItemRepository {
	// Logcat tag
	private static final String LOG = "DatabaseHelper";
	private static final String DATABASE_NAME = "ItemDb";
	private static final int DATABASE_VERSION = 1;
	// Table Names
	private static final String TABLE_Item = "Items";
	// Common column names
	private static final String KEY_ID = "_id";
	private static final String KEY_Name = "_name";
	private static final String KEY_Finished = "_finished";
	private static final String KEY_Priority = "_priority";
	// Table Create Statements
	// Item table create statement
	private static final String CREATE_TABLE_Item = "CREATE TABLE "
			+ TABLE_Item + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
			+ KEY_Name + " TEXT," + KEY_Finished + " INTEGER," + KEY_Priority
			+ " INTEGER" + ")";

	public ItemRepository(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		// Item Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// creating required tables
		db.execSQL(CREATE_TABLE_Item);
	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// Item Auto-generated method stub

	}

	@Override
	public List<Item> GetAll() {
		List<Item> Items = new ArrayList<Item>();
		String selectQuery = "SELECT * FROM " + TABLE_Item + " ORDER BY "
				+ KEY_Finished + " , " + KEY_Priority + " DESC";

		Log.e(LOG, selectQuery);

		SQLiteDatabase db = this.getReadableDatabase();
		Cursor c = db.rawQuery(selectQuery, null);

		// looping through all rows and adding to list
		if (c.moveToFirst()) {
			do {
				Item td = new Item();
				td.set_id(c.getInt((c.getColumnIndex(KEY_ID))));
				td.set_name((c.getString(c.getColumnIndex(KEY_Name))));
				td.set_priority(c.getInt(c.getColumnIndex(KEY_Priority)));
				if (c.getInt(c.getColumnIndex(KEY_Finished)) == 1) {
					td.set_finished(true);
				} else {
					td.set_finished(false);
				}

				// adding to Item list
				Items.add(td);
			} while (c.moveToNext());
			c.close();
		}

		return Items;
	}

	
	public void Add(Item item) {
		// Item Auto-generated method stub
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(KEY_Name, item.get_name());
		values.put(KEY_Priority, item.get_priority());
		values.put(KEY_Finished, item.get_finished());

		// insert row
		db.insert(TABLE_Item, null, values);
	}

	
	public void Update(Item item) {
		// Item Auto-generated method stub

		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(KEY_Name, item.get_name());
		values.put(KEY_Priority, item.get_priority());
		values.put(KEY_Finished, item.get_finished());

		// updating row
		db.update(TABLE_Item, values, KEY_ID + " = ?",
				new String[] { String.valueOf(item.get_id()) });
	}

	
	public void Delete(Item item) {
		// Item Auto-generated method stub
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(TABLE_Item, KEY_ID + " = ?",
				new String[] { String.valueOf(item.get_id()) });
	}

}
